home *** CD-ROM | disk | FTP | other *** search
/ JCSM Shareware Collection 1996 September / JCSM Shareware Collection (JCS Distribution) (September 1996).ISO / bother__ / cenvid.zip / CENVIDOS.ZIP / INSTALL.CMM < prev    next >
Text File  |  1995-04-01  |  14KB  |  381 lines

  1. /*************************************************************************
  2.  ***                         Install.cmm  ver.3                        ***
  3.  *** This is the installation program to set up CEnvi unregistered     ***
  4.  *** shareware on your computer.  Before running this program, CEnvi   ***
  5.  *** must be copied to a hard disk directory.  This Install.cmm        ***
  6.  *** program is identical for DOS, OS/2, and Windows version of CEnvi  ***
  7.  *** and this program is itself an example for how to use CEnvi        ***
  8.  *************************************************************************/
  9.  
  10. if defined(_WINDOWS_)
  11.    ExeName = "CEnviW.exe"
  12. else if defined(_DOS_)
  13.    ExeName = "CEnviD.exe"
  14. else if defined(_DOS32_)
  15.    ExeName = "CEnviD32.exe"
  16. else if defined(_OS2_)
  17.    ExeName = "CEnvi2.exe"
  18.  
  19. main(argc,argv)
  20. {
  21.    // There should be no input args.  If there are then the user needs help.
  22.    ScreenClear();
  23.    if ( argc != 1 )
  24.       GiveInstructionsAndExit();
  25.  
  26.    // Get the current directory.  The GetInstallDirectory() function assures
  27.    // that it is not a floppy.
  28.    InstallDirectory = GetInstallDirectory(argv[0]);
  29.  
  30.    if defined( _DOS_ ) || defined( _DOS32_ )
  31.       DosOrOS2Install(InstallDirectory,"AUTOEXEC.BAT","AUTOEXEC.BAK");
  32.    else if defined( _OS2_)
  33.       DosOrOS2Install(InstallDirectory,"CONFIG.SYS","CONFIG.BAK");
  34.    else {
  35.       assert( defined(_WINDOWS_) );
  36.       WindowsInstall(InstallDirectory);
  37.       if ( !defined(_SHELL_) ) {
  38.          printf("\nPress any key to exit...");
  39.          getch();
  40.       }
  41.    }
  42.    return(EXIT_SUCCESS);
  43. }
  44.  
  45.  
  46. GiveInstructionsAndExit()  // Show some info about using this program
  47. {                          // and then exit this program. Do not return.
  48.    printf("Install.cmm - CEnvi installation procedure.  Execute with no parameters\n");
  49.    printf("              from the directory that contains %s and also contains\n",ExeName);
  50.    printf("              the *.cmm and other sample CEnvi files, including Install.cmm\n");
  51.    if defined( _WINDOWS_ ) && !defined(_SHELL_) {
  52.       printf("\nPress any key to exit...");
  53.       getch();
  54.    }
  55.    exit(EXIT_FAILURE);
  56. }
  57.  
  58.  
  59. GetInstallDirectory(Executable)   // return current dir that is not on a floppy
  60. {
  61.    // current source directory from the name of the executable
  62.    CurDir = SplitFileName(FullPath(Executable)).dir;
  63.    assert( CurDir != NULL  &&  CurDir[0] != 0 );
  64.  
  65.    // check that current directory is not floppy A: or B:
  66.    if ( CurDir[0] == 'A'  ||  CurDir[0] == 'B' ) {
  67.       printf("\nCannot install CEnvi from a floppy.\n\a\n");
  68.       GiveInstructionsAndExit();
  69.    }
  70.  
  71.    // Have found the directory Install.cmm is in, and so check that
  72.    // CEnvi.exe is also in the same directory.
  73.    if ( NULL == Directory(strcat(strcpy(temp,CurDir),ExeName)) ) {
  74.       printf("\nThis installation assumes that %s is in the same directory as\n",ExeName);
  75.       printf("Install.cmm.  Could not find %s in the Install.cmm directory.\n\a\n",ExeName);
  76.       GiveInstructionsAndExit();
  77.    }
  78.  
  79.    // remove extra backslash if at end of name (i.e., not root directory)
  80.    if ( strcmp(CurDir+1,":\\") )
  81.       CurDir[strlen(CurDir)-1] = 0;
  82.  
  83.    // return the result
  84.    return(CurDir);
  85. }
  86.  
  87.  
  88. Fatal(Format)  // like printf() but also beeps and exits program
  89. {
  90.    va_start(parameters,Format);
  91.    printf("\a\n");  // beep
  92.    vprintf(Format,parameters);
  93.    exit(EXIT_FAILURE);
  94. }
  95.  
  96.  
  97. GetYesOrNo()   // prompt for Yes or No, and return entered boolean TRUE
  98. {              // if YES else FALSE if NO.
  99.    printf(" (Y/N) ");
  100.    while( TRUE ) { // forever
  101.       key = toupper(getch());
  102.       if ( key != 'Y' && key != 'N' )
  103.          printf("\a");  // beep
  104.       else
  105.          break;
  106.    }
  107.    printf("%c\n",key);
  108.    return( key == 'Y' );
  109. }
  110.  
  111.  
  112. CopyFile(Source,Destination) // copy file from source to destination
  113. {
  114.    // open source and destination files
  115.    src = fopen(Source,"rb");
  116.    if ( src == NULL )
  117.       Fatal("Could not open source file \"%s\" for reading.",Source);
  118.    dest = fopen(Destination,"wb");
  119.    if ( dest == NULL )
  120.       Fatal("Could not open file \"%s\" for writing.",Destination);
  121.  
  122.    // read chunks from source, and copy to destination
  123.    #define  COPY_CHUNK_SIZE   500
  124.    while ( 0 != (size = fread(buf,COPY_CHUNK_SIZE,src)) ) {
  125.       fwrite(buf,size,dest);
  126.    }
  127.  
  128.    // close the files
  129.    fclose(dest);
  130.    fclose(src);
  131. }
  132.  
  133.  
  134. SkipWhitespace(str)
  135. {
  136.    while( isspace(str[0]) )
  137.       str++;
  138. }
  139.  
  140.  
  141. AlreadyInPath(Dir,Path) // search through path for this Dir, return True if found, else False
  142. {
  143.    len = strlen(Dir)
  144.    p = Path;
  145.    do {
  146.       if ( 0 == strnicmp(p,Dir,len)  && (p[len]==0 || p[len]==';') )
  147.          return(True)
  148.       p = strchr(p,';')
  149.    } while( p++ != NULL )
  150.    return(False)
  151. }
  152.  
  153.  
  154. AlterEVarLine(text,EVar,Dir)
  155.    // If this text is a line setting the EVar environment variable, and if
  156.    // Dir is not already in it, then add Dir.  Return TRUE if this is
  157.    // a line for EVar, and False if it is not.
  158. {
  159.    FoundEVar = FALSE; // assume this is not the line
  160.    // determine if text is of the type "EVAR=" or "set EVAR="
  161.    SkipWhitespace(c = text);
  162.    // if the next statement is "SET" then skip it
  163.    if ( !strncmpi(c,"SET",3) ) {
  164.       // Skip beyond the SET statement
  165.       c += 3;
  166.       SkipWhitespace(c);
  167.    }
  168.    // test if this next part is the variable name
  169.    EVarLen = strlen(EVar);
  170.    if ( !strncmpi(c,EVar,EVarLen) ) {
  171.       c += EVarLen;
  172.       if ( isspace(c[0])  ||  c[0] == '=' ) {
  173.          // THIS IS IT.  This line describes the EVar.
  174.          SkipWhitespace(c);
  175.          if ( c[0] == '=' ) {
  176.             c++;
  177.             SkipWhitespace(c);
  178.          }
  179.          FoundEVar = TRUE;
  180.          // If Dir is not already in this line then add Dir at the end.
  181.          // Check each dir as value between semicolons (;)
  182.          SkipWhitespace(c);
  183.          if ( !AlreadyInPath(Dir,c) ) {
  184.             // add this to the end of the existing path
  185.             if ( c[strlen(c)-1] != ';' )
  186.                strcat(c,";");
  187.             strcat(c,Dir);
  188.          }
  189.       }
  190.    }
  191.    return(FoundEVar);
  192. }
  193.  
  194. DosOrOS2Install(Dir,pFileName,pBackupFileName)
  195. {
  196.    // append the PATH statement so that it contains this directory, and
  197.    // also add the CMMPATH environment variable.  But give user a choice
  198.    // of whether to do this first; if they choose not to then tell them
  199.    // what they must do by hand.
  200.    printf("\nIf you choose, install will add the directory:\n\n");
  201.    printf("    \"%s\"\n\n",Dir);
  202.    printf("to the PATH environment variable in your %s file.  Install will\n",pFileName);
  203.    printf("also add this directory to the CMMPATH environment variable in that\n");
  204.    printf("file.  If you select to make these changes to %s, then\n",pFileName);
  205.    printf("install will backup the current version of %s to %s.\n",pFileName,pBackupFileName);
  206.    printf("\n\nShould these changes be made to %s?",pFileName);
  207.    if ( GetYesOrNo() ) {
  208.  
  209.       printf("Enter drive letter for the booted %s file: ",pFileName);
  210.       DriveLetter = getche();
  211.       printf("\n");
  212.       if ( !isalpha(DriveLetter) ) {
  213.          printf("\aInvalid drive letter %c!\n",DriveLetter);
  214.          exit(EXIT_FAILURE);
  215.       }
  216.       sprintf(lFileName,"%c:\\%s",DriveLetter,pFileName);
  217.       sprintf(lBackupFileName,"%c:\\%s",DriveLetter,pBackupFileName);
  218.  
  219.       printf("Making changes to %s...",lFileName);
  220.       // user chose to make automatic changes.  Do so now
  221.       CopyFile(lFileName,lBackupFileName);
  222.  
  223.       // will now read the backup file, and if any PATH or CMMPATH line is
  224.       // encountered then will alter it, else just copy line exactly.
  225.       src = fopen(lBackupFileName,"rt");
  226.       if ( src == NULL )
  227.          Fatal("Could not open source file \"%s\" for reading.",lBackupFileName);
  228.       dest = fopen(lFileName,"wt");
  229.       if ( dest == NULL )
  230.          Fatal("Could not open source file \"%s\" for reading.",lBackupFileName);
  231.       SetPath = SetCmmPath = False;
  232.       while ( NULL != (line = fgets(src)) ) {
  233.          // remove the pesky newline if there is one
  234.          if ( PeskyNewLine = (line[strlen(line)-1] == '\n') )
  235.             line[strlen(line)-1] = 0;
  236.          if ( AlterEVarLine(line,"PATH",Dir) )
  237.             SetPath = True;
  238.          if ( AlterEVarLine(line,"CMMPATH",Dir) )
  239.             SetCmmPath = True;
  240.          fputs(line,dest);
  241.          if ( PeskyNewLine )
  242.             fputs("\n",dest);
  243.       }
  244.       fclose(src);
  245.       // if haven't already written PATH or CMMPATH, then do so now
  246.       if ( !SetPath )
  247.          fprintf(dest,"\nSET PATH=%s\n",Dir);
  248.       if ( !SetCmmPath )
  249.          fprintf(dest,"\nSET CMMPATH=%s\n",Dir);
  250.       fclose(dest);
  251.       printf("\n%s has been altered.\n",lFileName);
  252.       printf("Changes will take effect after you reboot.\n");
  253.    } else {
  254.       // user choose not to automatically install, so describe what the user
  255.       // needs to do by hand to make it work.
  256.       ScreenClear();
  257.       printf("\nThe PATH environment variable is used to find executable files, such as\n");
  258.       printf("%s, and batch files, such as those included in this CEnvi\n",ExeName);
  259.       printf("unregistered shareware release.  The CMMPATH environment variable is\n");
  260.       printf("used by CEnvi to find common source code that may be included\n");
  261.       printf("in other CEnvi source files.\n\n");
  262.       printf("You chose not to have Install automatically alter %s.\n\n",pFileName);
  263.       printf("You may wish to try Install.cmm again, or to make these modifications\n");
  264.       printf("to %s by hand.\n",pFileName);
  265.    }
  266. }
  267.  
  268.  
  269. WindowsInstall(Dir)
  270. {
  271.    // If the user chooses, then add CMM Extensions and also add
  272.    // CMMPATH setting to WIN.INI.
  273.    printf("\nIf you choose, install will add the directory:\n\n");
  274.    printf("    \"%s\"\n\n",Dir);
  275.    printf("as a CMMPATH profile string in your WIN.INI file.  Install will also\n");
  276.    printf("also add the .CMM extension to WIN.INI so that you can execute any\n");
  277.    printf("*.CMM file simply by double-clicking on it.\n");
  278.    printf("\n\nShould these changes be made to WIN.INI?");
  279.    if ( GetYesOrNo() ) {
  280.       printf("Making changes to WIN.INI...");
  281.       // add extensions
  282.       sprintf(extension,"%s\\%s ^.CMM",Dir,ExeName);
  283.       WriteProfileString("Extensions","CMM",extension);
  284.       // add CMMPATH
  285.       WriteProfileString("CEnvi","CMMPATH",Dir);
  286.       TellEveryoneThatWinIniHasChanged();
  287.       printf("\n");
  288.    } else {
  289.       // user choose not to automatically install, so describe what the user
  290.       // needs to do by hand to make it work.
  291.       ScreenClear();
  292.       printf("The .CMM extension in WIN.INI would allow you to execute CMM code simply by\n");
  293.       printf("double-clicking the file name.  The CMMPATH profile string allows CEnvi to\n");
  294.       printf("find Cmm source code or code library that may be in a different directory.\n");
  295.       printf("\n");
  296.       printf("You chose not to have Install automatically alter WIN.INI\n");
  297.       printf("\n");
  298.       printf("You may wish to make these changes by hand; if so, then add the line:\n");
  299.       printf("     CMM=%s\\%s ^.CMM\n",Dir,ExeName);
  300.       printf("to the [Extensions] section of WIN.INI.  At the end of WIN.INI you may also\n");
  301.       printf("want to add these lines:\n");
  302.       printf("    [CEnvi]\n");
  303.       printf("    CMMPATH=%s\n",Dir);
  304.       exit(0);
  305.    }
  306.    // If the user chooses, then add CMM Extensions and also add
  307.    // CMMPATH setting to WIN.INI.
  308.    printf("\nIf you choose, install will add a CEnvi Program Group.\n\n");
  309.    printf("It will include an icon for the CEnvi for Windows program,\n");
  310.    printf("and the quick start samples which may then be ran as programs.\n");
  311.    printf("\n\nShould the CEnvi QuickStart program group be created?");
  312.    if ( GetYesOrNo() ) {
  313.       printf("Creating CEnvi QuickStart program group...");
  314.       spawn(P_WAIT,`CEnviW.exe "#include <Install.cmm,,//QSG>"`);
  315.    }
  316. }
  317.  
  318. WriteProfileString(ApplicationName,KeyName,KeyValue)
  319.    // This is a wrapper for the WriteProfileString in the Windows Kernel.
  320. {
  321.    DynamicLink("KERNEL","WRITEPROFILESTRING",SWORD16,PASCAL,
  322.                ApplicationName,KeyName,KeyValue);
  323. }
  324.  
  325.  
  326. TellEveryoneThatWinIniHasChanged()
  327. {
  328.    #define HWND_BROADCAST  0xFFFF
  329.    #define WM_WININICHANGE 0x001A
  330.    DynamicLink("USER","SENDMESSAGE",SWORD16,PASCAL,
  331.                HWND_BROADCAST,WM_WININICHANGE,0,0,0);
  332. }
  333.  
  334. // FOLLOWING CODE IS USED BY WINDOWS INSTALL IF QUICK-START
  335. // PROGRAM GROUP IS TO BE CREATED
  336. //
  337. //QSG   #define main AddItemMain
  338. //QSG   #include <AddItem.cmm>
  339. //QSG
  340. //QSG   // add the CEnvi program item, and the Windows Quick Start samples.
  341. //QSG   MyAddItem(``,`Cenvi For Windows`);
  342. //QSG
  343. //QSG   MyAddItem(`bmpview.cmm`,`BmpView.cmm`);
  344. //QSG   MyAddItem(`dropper.cmm NOTEPAD /Title=DropNotes /Icon=NOTEPAD.EXE`,
  345. //QSG           `Dropper.cmm <NotePad>`,`NOTEPAD.EXE`);
  346. //QSG   MyAddItem(`exitwin.cmm DOS`,`ExitWin.cmm <Exit to DOS>`,
  347. //QSG             `PROGMAN.EXE`,1);
  348. //QSG   MyAddItem(`inputbox.cmm`,`InputBox.cmm`);
  349. //QSG   MyAddItem(`keyghost.cmm`,`KeyGhost.cmm`);
  350. //QSG   MyAddItem(`msgboxes.cmm`,`MsgBoxes.cmm`);
  351. //QSG   MyAddItem(`pm_exits.cmm`,`PM_Exits.cmm`,`PROGMAN.EXE`);
  352. //QSG   MyAddItem(`pmbutt.cmm`,`PMButt.cmm`,`PROGMAN.EXE`);
  353. //QSG   MyAddItem(`pongtime.cmm`,`PongTime.cmm`,`CLOCK.EXE`);
  354. //QSG   MyAddItem(`wintools.cmm`,`WinTools.cmm`);
  355. //QSG   MyAddItem(`MenuNot.cmm Program File##Run`,
  356. //QSG             `MenuNot.cmm <Disable PROGMAN File/Run>`,`PROGMAN.EXE`);
  357. //QSG
  358. //QSG   GetMyExeSpec() // return full path for current CEnviW instance
  359. //QSG   {
  360. //QSG      ExeName[0] = ExeName[400] = '\0';
  361. //QSG      DynamicLink("KERNEL","GETMODULEFILENAME",SWORD16,PASCAL,
  362. //QSG                  Instance(),ExeName,399);
  363. //QSG      return ExeName;
  364. //QSG   }
  365. //QSG
  366. //QSG   MyAddItem(pCommand,pName,pIcon,pIconIndex)
  367. //QSG   {
  368. //QSG      gargv[0] = "AddItem.cmm";
  369. //QSG      gargv[1] = "CEnvi QuickStart";
  370. //QSG      sprintf(gargv[2],"%s %s",GetMyExeSpec(),pCommand);
  371. //QSG      sprintf(gargv[3],"/NAME=%s",pName);
  372. //QSG      if ( 2 < va_arg() ) {
  373. //QSG         sprintf(gargv[4],"/ICON=%s",pIcon);
  374. //QSG         if ( 3 < va_arg() )
  375. //QSG            sprintf(gargv[5],"/ICONINDEX=%d",pIconIndex);
  376. //QSG      }
  377. //QSG      AddItemMain(va_arg() + 2,gargv);
  378. //QSG   }
  379.  
  380.  
  381.